package com.example.ximalayademo.presenters;

import com.example.ximalayademo.api.XimalayaApi;
import com.example.ximalayademo.interfaces.IRecommendPresenter;
import com.example.ximalayademo.interfaces.IRecommendViewCallback;
import com.example.ximalayademo.utils.Constants;
import com.example.ximalayademo.utils.LogUtil;
import com.ximalaya.ting.android.opensdk.constants.DTransferConstants;
import com.ximalaya.ting.android.opensdk.datatrasfer.CommonRequest;
import com.ximalaya.ting.android.opensdk.datatrasfer.IDataCallBack;
import com.ximalaya.ting.android.opensdk.model.album.Album;
import com.ximalaya.ting.android.opensdk.model.album.GussLikeAlbumList;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RecommendPresenter implements IRecommendPresenter {

    private static final String TAG = "RecommendPresenter";

    private List<IRecommendViewCallback> mCallbacks = new ArrayList<>();
    private List<Album> mCurrentRecommend = null;

    private RecommendPresenter(){};

    /**
     * 获取单例对象
     * @return
     */
   private volatile static RecommendPresenter sInstance = null;
   public static RecommendPresenter getInstance(){
       if (sInstance == null) {
           synchronized (RecommendPresenter.class){
               if (sInstance == null){
                   sInstance = new RecommendPresenter();
               }
           }
       }
       return sInstance;
   }

    /**
     * 获取当前的推荐专辑
     *
     * @return 使用之前要判空
     */
   public  List<Album> getCurrentRecommend(){
       return mCurrentRecommend;
   }

    /**
     * 获取推荐内容，其实就是猜你喜欢
     * 这个接口：3.10.6
     */
    @Override
    public void getRecommendList() {
        //获取推荐内容
        //封装参数
        updataLoading();
        XimalayaApi ximalayaApi = XimalayaApi.getXimalayaApi();
        ximalayaApi.getRecommendList(new IDataCallBack<GussLikeAlbumList>() {
            @Override
            public void onSuccess(GussLikeAlbumList gussLikeAlbumList) {
                LogUtil.d(TAG , "thread ----> "+Thread.currentThread().getName());
                //数据获取成功
                if (gussLikeAlbumList != null) {
                    List<Album> albumList = gussLikeAlbumList.getAlbumList();
                    //数据回来以后，我们要求更新UI
                    //upRecommendUI(albumList);
                    handlerRecommendResult(albumList);
                }
            }
            //获取数据失败
            @Override
            public void onError(int i, String s) {
                LogUtil.d(TAG, "error code ----> "+i);
                LogUtil.d(TAG, "errorMsg ----> "+s);
                handlerError();
            }
        });
    }

    @Override
    public void push2RefreshMore() {

    }

    @Override
    public void loadMore() {

    }

    private void handlerError() {
        if (mCallbacks != null) {
            for (IRecommendViewCallback callback : mCallbacks){
                callback.onNetworkError();
            }
        }
    }

    private void handlerRecommendResult(List<Album> albumList) {
        //通知UI更新
        if (albumList != null) {
            if (albumList.size() == 0) {
                for (IRecommendViewCallback callback : mCallbacks){
                    callback.onEmpty();
                }
            }else {
                for (IRecommendViewCallback callback : mCallbacks){
                    callback.onRecommendListLoaded(albumList);
                }
                this.mCurrentRecommend = albumList;
            }
        }
    }

    private void updataLoading(){
        for (IRecommendViewCallback callback : mCallbacks){
            callback.onLoading();
        }
    }

    @Override
    public void registerViewCallBack(IRecommendViewCallback callback) {
       if (mCallbacks != null && !mCallbacks.contains(callback)){
           mCallbacks.add(callback);
       }
    }

    @Override
    public void unRegisterViewCallBack(IRecommendViewCallback callback) {
        if (mCallbacks != null && mCallbacks.contains(callback)) {
            mCallbacks.remove(callback);
        }
    }
}
